package jianzhi_offer;

import java.util.*;

public class _33_第一个只出现一次的字符 {
    public static void main(String[] args) {
        _33_第一个只出现一次的字符 test = new _33_第一个只出现一次的字符();
        System.out.println(test.FirstNotRepeatingChar("google"));
    }
    public int FirstNotRepeatingChar(String str) {
        if(str == null || str.length() == 0) return -1;
        char[] chars = str.toCharArray();
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        Set<Character> set = new HashSet<>();
        for(int i=0;i<str.length();++i){
            if(set.contains(chars[i])) continue;
            Integer count = map.get(chars[i]);
            if(count == null){
                map.put(chars[i],i);//以前没有就存
            }else{
                map.remove(chars[i]);
                set.add(chars[i]);
            }
        }
        if(map.size() == 0) return -1;
        int min = Integer.MAX_VALUE;
        for(int i:map.values()) min = Math.min(min,i);
        return min;
    }
}
